include<ub.scad>; //⇒ v.gd/ubaer or https://github.com/UBaer21/UB.scad
/*[Hidden]*/
designVersion="2.2";
designer="Ulrich Bär";
license="CC0";
useVersion=23.080;//(sites.google.com/site/ulrichbaer)
assert(Version>=useVersion,str("lib version ",Version," detected, include ",useVersion," ub.scad library‼ ⇒http://v.gd/ubaer"));
/*[Basics]*/
nozzle=.2;
bed=false;
pPos=[0,0];
info=false;
name=undef;

/*[Clamp]*/
// max clamping depth
size=20;
// max clamping length
length=30;
// clamping width
width=0;
// screw diameter
dia=6;//[6,10]
//interlock tip
tip=false;
// lock notches
lock=true;
// knurling dist on tongs
knurl=1.5;

//taper base
taper=6.75;//[0,6.75,10.5]
// taper length
taperL=4.5;//[4.5,8]

T(printPos){

Color(.15,spread=.15,l=.85,v=.9){
  PrevPos(rot=[180],z=5+dia+spiel*2)Clamp(length,size,dia,opt=0); // fix jaw
  PrevPos(rot=[0,0,180],t=[0,10.2,0.1],tP=[(size)-5*2,15+taperL])rotate(180)Cut(1,t=[-10,15],cut=[-1],ghost=0)Clamp(length,size,dia,opt=1);//move jaw
  PrevPos(rot=0,t=[0,-7.5,4],tP=[dia+3,-length- dia-10])Clamp(length,size,dia,opt=2);// spindle
}


}

module Clamp (l=20,size=20,d=5,h=width,p=1.5,opt=0,dicke=5.0,taper=taper,taperL=taperL,lock=lock,tip=tip,knurl=knurl){

size=[size+d/2,dicke];
h=max(d+spiel*2+dicke,is_num(h)?h:0);
hSpindle=sin(35)*d;
block=[d+dicke*2+spiel*2,p*4+1];
l=l+block.y;
railH=h/3*2;
steps=1.5;
stepH=1;
stepDeg=4;

//lock
if(tip&&opt!=2)T(size.x+spiel)Roof(h/2-spiel,.5*[1,1]){
  T(y=-3)Quad(2.5,size.y+3,rad=.5,center=[0,0]);
  T(y=.5)Quad(2.5*2,size.y-.5,rad=.5,center=[1,0]);
  }

//taper
if(opt==1&&taper)T(size.x-taper/2-1,dicke-.5)Halb()Tz((taper/2-tan(2)*taperL)*sin(60))R(-90)Kegel(d1=taper,h=taperL,rad=[0,.25],grad=90-2);
if(opt==0&&taper)T(size.x-taper/2-1,dicke-.5)Halb()Tz((taper/2-tan(2)*taperL)*sin(60))R(-90)Kegel(d1=taper,h=taperL,rad=[0,.25],grad=90-2);

//rail
if(opt==0)difference(){
  Roof(railH,[1,1]*.5)T(-block.x/2-spiel,block.y)Quad(dicke,l+block.y,rad=1,center=[-1,-1]);
  if(lock)T(0,-1)Tz(railH)rotate(180)Linear(l/steps,es=steps,y=1)R(0,90,0)LinEx(200,center=true)Vollwelle(r=.25,h=stepH,grad=95,extrude=0,xCenter=-1,x0=-1,tMitte=stepH*tan(stepDeg)*4+spiel+.5);

}
//connect gap
if(opt==1){
  T(-block.x/2+2,block.y)Roof(h-railH-spiel,[1,1]*.5)Quad(dicke+3+2+spiel*2,block.y,rad=1,center=[-1,-1]);
  if(lock)T(-(block.x/2+dicke/2+spiel),block.y/2,h-railH-spiel)Linear(floor((block.y -1)/steps),center=true,es=steps,y=1)R(0,-90,0)LinEx(dicke+spiel*2+1,center=true)Vollwelle(r=.25,h=stepH,grad=95,extrude=0,xCenter=-1,x0=-1,tMitte=stepH*tan(stepDeg)*2+.5);
}
if(opt==0||opt==1){
difference(){
    Roof(h,[1,1]*.5)Rund(0,0.5){
      Rund(0,2.5){
        Quad(size,rad=.5,center=[0,0]);// jaw
        Quad(block,rad=1,center=[1,0]);// block
        
      }
      if(opt==0)T(-block.x/2-spiel,block.y)Quad(dicke,block.y,rad=1,center=[-1,-1]);
      if(opt==1)T(-block.x/2-spiel-dicke-spiel,block.y)Quad(3,block.y*2,rad=1,center=[-1,-1]);
    }
    Tz(h/2){
    cube();
      R(90){
        //cylinder(d=d+spiel*2,h=100,center=true,$fn=0);
        LinEx(100,center=true)Tdrop(d=d+spiel*2);
        Tz(-block.y/2)MKlon(tz=-block.y/2-.1)Kegel(d1=d+spiel*2+1);
       }
// knurl       
      if(knurl)T(d/2+4-knurl/2,z=-knurl/2)intersection(){
        Linear(20,es=knurl,center=0)R(0,($idx%2?-45:45))cylinder(100,d=1.5,center=true,$fn=4);
        cube([100,1,100],true);
      }
    }
  }


if(opt==1)T(y=.5)Tz(h/2)R(-90)intersection(){
    rotate(90)Gewinde(dn=d+spiel*2,innen=true,p=p,grad=360*3,winkel=30,ratio=.9,rund=true,center=false,wand=1);
    difference(){
      cube([20,h,100],true);
      rotate(-90)LinEx(100,center=true)Tri(c=d -1.5,grad=50);
    }
  }
}

// Spindel

if(opt==2)union(){
  T(z=hSpindle/2)R(-90)difference(){
    union(){
      Tz(block.y-1)Gewinde(dn=d,h=l,winkel=30,p=p,ratio=.9,rund=true,center=false,wand=0);
      Tz(l+block.y-1)scale([1,1,.65])sphere($fn=0,d=d-1.9);
      Tz(-.5)Pille(d=d,l=block.y-1,center=false,rad=[-1.5,1]);
    }
    MKlon(ty=hSpindle/2+25)cube([20,50,l*2+30],true);
  }
  Roof(hSpindle,[1,1]*.5)Rund(0,1.5){
    Quad(Kathete(d/2,hSpindle/2-0.4)*2,block.y ,center=[1,0]);
    Quad(Kathete(d/2-1,hSpindle/2-0.5)*2,block.y+5,center=[1,0],fn=50);
    T(y=-2.5)Quad(20,5);// grip
  }
}


}

// version Info
if(string2num(designVersion[2])>0)T(1,-1)color("navy")linear_extrude(.1)Seg7(str(designVersion),h=1,spiel=0.01,b=.05,ratio=0.5,center=true,name=0);